<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head th:include="header">
    <title>角色管理-列表</title>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>角色管理</h5>
                    </div>
                    <div class="ibox-content">
                        <div class="row row-lg">
                            <div class="col-sm-12">
                                <div class="row">
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="roleName" placeholder="角色名称" />
                                    </div>
                                    <div class="col-sm-3">
                                        <button type="submit" class="btn btn-sm btn-primary" onclick="Role.search()">
                                            <i class="fa fa-search">搜索</i>
                                        </button>
                                    </div>
                                </div>
                                <div class="hidden-xs" id="roleTableToolbar" role="group">
                                    <button shiro:hasPermission="/role/add" class="btn btn-sm btn-primary" type="submit" onclick="Role.openAddRole()">
                                        <i class="fa fa-pencil"></i>添加</button>
                                    <button shiro:hasPermission="/role/edit" class="btn btn-sm btn-info" type="submit" onclick="Role.openChangeRole()">
                                        <i class="fa fa-paste"></i>修改</button>
                                    <button shiro:hasPermission="/role/delete" class="btn btn-sm btn-danger" type="submit" onclick="Role.delRole()">
                                        <i class="fa fa-cut"></i>删除</button>
                                    <button shiro:hasPermission="/role/grant" class="btn btn-sm btn-warning" onclick="Role.assign()">
                                        <i class="fa fa-edit"></i>权限配置</button>
                                </div>
                                <table id="roleTable" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div th:include="footer"></div>
    <script type="text/javascript" th:inline="javascript">
        /**
        * 角色管理的单例
        */
        var Role = {
            id: "roleTable",	//表格id
            seItem: null,		//选中的条目
            table: null,
            layerIndex: -1
        };

        /**
         * 初始化表格的列
         */
        Role.initColumn = function () {
            var columns = [
                { field: 'selectItem', radio: true },
                { title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle' },
                { title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true },
                { title: '排序', field: 'seq', align: 'center', valign: 'middle', sortable: true },
                {
                    title: '状态', field: 'status', align: 'center', valign: 'middle', sortable: true, formatter: function (value, row, index) {
                        switch (value) {
                            case 0:
                                return '正常';
                            case 1:
                                return '停用';
                        }
                    }
                },
                { title: '简介', field: 'description', align: 'center', valign: 'middle', sortable: true },
                {
                    title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true
                },
                {
                    title: '更新时间', field: 'updateTime', align: 'center', valign: 'middle', sortable: true
                }]
            return columns;
        };


        /**
         * 检查是否选中
         */
        Role.check = function () {
            var selected = $('#' + this.id).bootstrapTable('getSelections');
            if (selected.length == 0) {
                hdw.info("请先选中表格中的某一记录！");
                return false;
            } else {
                Role.seItem = selected[0];
                return true;
            }
        };

        /**
         * 点击添加角色
         */
        Role.openAddRole = function () {
            var index = layer.open({
                type: 2,
                title: '添加角色',
                area: ['35%', '75%'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: basePath + 'role/addPage'
            });
            this.layerIndex = index;
        };

        /**
         * 点击修改按钮时
         */
        Role.openChangeRole = function () {
            if (this.check()) {
                var index = layer.open({
                    type: 2,
                    title: '修改角色',
                    area: ['35%', '75%'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: basePath + 'role/editPage/' + this.seItem.id
                });
                this.layerIndex = index;
            }
        };

        /**
         * 删除角色
         */
        Role.delRole = function () {
            if (this.check()) {

                var operation = function () {
                    var ajax = new $ax(basePath + "/role/delete", function (data) {
                        if (data.success) {
                            hdw.success("删除成功!");
                        } else {
                            hdw.success("删除失败!");
                        }
                        Role.table.refresh();
                    }, function (data) {
                        hdw.error("删除失败!" + data.responseJSON.message + "!");
                    });
                    ajax.set("roleId", Role.seItem.id);
                    ajax.start();
                };

                hdw.confirm("是否删除角色 " + Role.seItem.name + "?", operation);
            }
        };

        /**
         * 权限配置
         */
        Role.assign = function () {
            if (this.check()) {
                var index = layer.open({
                    type: 2,
                    title: '权限配置',
                    area: ['35%', '90%'], //宽高
                    fix: false, //不固定
                    maxmin: true,
                    content: basePath + 'role/grantPage/' + this.seItem.id
                });
                this.layerIndex = index;
            }
        };

        /**
         * 搜索角色
         */
        Role.search = function () {
            var queryData = {};
            queryData['roleName'] = $("#roleName").val();
            Role.table.refresh({ query: queryData });
        }

        $(function () {
            var defaultColunms = Role.initColumn();
            var table = new BSTable(Role.id, basePath + "role/dataGrid", defaultColunms);
            table.setPaginationType("server");
            table.init();
            Role.table = table;
        });
    </script>
</body>

</html>